package com.qlylkj.lbk.ui.home

import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.forest.core.etx.*
import com.ms.banner.Banner
import com.ms.banner.BannerConfig
import com.qlylkj.lbk.R
import com.qlylkj.lbk.app.addHost
import com.qlylkj.lbk.base.WebViewActivity
import com.qlylkj.lbk.model.bean.*
import com.qlylkj.lbk.ui.cochain.CoChainInfoActivity
import com.qlylkj.lbk.ui.news.NewsActivity

class HomeAdapter(data: MutableList<HomeViewType>) :
    BaseMultiItemQuickAdapter<HomeViewType, BaseViewHolder>(data) {

    init {
        addItemType(HomeViewType.TYPE_BANNER, R.layout.item_banner)
        addItemType(HomeViewType.TYPE_COLUMN_MORE, R.layout.item_column_more)
        addItemType(HomeViewType.TYPE_NEWS, R.layout.item_news)
        addItemType(HomeViewType.TYPE_CO_CHAIN_INFO, R.layout.item_co_chain_info)
    }


    override fun convert(helper: BaseViewHolder, item: HomeViewType?) {
        helper.run {
            item?.run {
                when (itemType) {
                    HomeViewType.TYPE_BANNER -> {
                        val bannerList = itemAny as BannerList
                        val homeBannerView = getView<Banner>(R.id.homeBanner)
                        mContext?.run {
                            homeBannerView.apply {
                                val bannerWidth = screenWidth - dp2px(28)
                                val bannerHeight = bannerWidth / 2
                                layoutParams.width = bannerWidth
                                layoutParams.height = bannerHeight
                                setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
                                setPages(bannerList.records) { context, _, data ->
                                    val homeBanner = data as HomeBanner
                                    val bannerView = context.inflate(R.layout.item_banner_home)
                                    val bannerImage =
                                        bannerView.findViewById<ImageView>(R.id.bannerImage)
                                    bannerImage.load(
                                        context,
                                        homeBanner.faceImg.addHost(),
                                        RequestOptions().placeholder(R.mipmap.ic_conver_default)
                                    )
                                    val bannerTitle =  bannerView.findViewById<TextView>(R.id.bannerTitle)
                                    bannerTitle.text = homeBanner.title
                                    return@setPages bannerView
                                }
                                setOnBannerClickListener { bannerList, position ->
                                    val homeBanner = bannerList[position] as HomeBanner
                                    homeBanner.apply {
                                        startKtxActivity<WebViewActivity>(
                                            values = mutableListOf(
                                                WebViewActivity.TITLE to title,
                                                WebViewActivity.URL to jumpUrl.addHost()
                                            )
                                        )
                                    }
                                }
                                start()
                            }
                        }
                    }
                    HomeViewType.TYPE_COLUMN_MORE -> {
                        val columnName = itemAny as String
                        setText(R.id.titleText, columnName)
                        getView<TextView>(R.id.moreText).setOnClickListener {
                            when (columnName) {
                                "链改新闻" -> {
                                    mContext.startKtxActivity<NewsActivity>()
                                }
                                "最新上链信息" -> {
                                    mContext.startKtxActivity<CoChainInfoActivity>()
                                }
                                else -> {
                                }
                            }

                        }
                    }
                    HomeViewType.TYPE_NEWS -> {
                        val news = itemAny as News
                        news.apply {
                            val newsImage = getView<ImageView>(R.id.newsImage)
                            newsImage.load(
                                mContext,
                                faceImg.addHost(),
                                RequestOptions().placeholder(R.mipmap.ic_conver_default)
                            )
                            setText(R.id.newsTitleText, title)
                            setText(R.id.newsTimeText, createTime)
                        }
                    }
                    HomeViewType.TYPE_CO_CHAIN_INFO -> {
                        val coChainInfo = itemAny as CoChainInfo
                        coChainInfo.apply {
                            consumers.apply {
                                val coChainCompanyCoverImage = getView<ImageView>(R.id.coChainCompanyCoverImage)
                                coChainCompanyCoverImage.load(
                                    mContext,
                                    faceImg.addHost(),
                                    RequestOptions().placeholder(R.mipmap.ic_company_cover_default)
                                )
                                setText(R.id.companyNameText, companyName)
                            }
                            setText(R.id.transactionHashText, transactionHash)
                            setText(R.id.timeText, createTime)
                        }
                    }
                    else -> {
                    }
                }
            }
        }
    }
}